#include <iostream>
#include <string.h>
using std::cout;
using std::endl;

/*
 * 要求此版本仅能访问堆对象
 * */

class Computer{
private:
    Computer(const char* brand, int price)
        :_brand(new char[strlen(brand) + 1]())
        ,_price(price)
    {
        strcpy(_brand, brand);
        printf("构造函数\n");
    }

    ~Computer(){
        if(_brand != nullptr){
            delete[] _brand;
            _brand = nullptr;
        }
        printf("析构函数\n");
    }

    char* _brand;
    int _price;

public:
    static Computer *create(const char* brand, int price){
        return new Computer(brand, price);
    }

    static void destroy(Computer *ptr){
        delete ptr;
    }

    Computer(const Computer & rhs)
        :_brand(new char[strlen(rhs._brand) + 1]())
        ,_price(rhs._price)
    {
        strcpy(_brand, rhs._brand);
        printf("拷贝函数\n");
    }

    Computer & operator=(const Computer &rhs){
        printf("赋值运算\n");
        if(this != &rhs){
            delete [] _brand;
            _brand = new char[strlen(rhs._brand) + 1]();
            strcpy(_brand, rhs._brand);
            _price = rhs._price;
        }
        return *this;
    }

    void print(){
        printf("brand: %s, price: %d \n", _brand, _price);
    }
};

int main()
{
    Computer *cpt1 = Computer::create("apple", 1000);
    cpt1->print();
    Computer::destroy(cpt1);

    Computer *cpt2 = Computer::create("xiaomi", 2000);
    cpt2->print();
    Computer::destroy(cpt2);

    return 0;
}

